package com.zhongxi.guoyuan.OffLineShop.ConnectNet;

import com.zhongxi.guoyuan.OffLineShop.ConnectNet.Api.OffLineApi;
import com.zhongxi.guoyuan.OffLineShop.Utils.LogInterceptor;
import com.zhongxi.guoyuan.url.Url;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2018/1/5.
 */

public class NetWork {
    private static OffLineApi offLineApi;
    private static final long CONNECT_TIMEOUT_TIME = 10;

    private static OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(new LoggerInterceptor("NetWork", true))
            .connectTimeout(CONNECT_TIMEOUT_TIME, TimeUnit.SECONDS)
            .readTimeout(CONNECT_TIMEOUT_TIME, TimeUnit.SECONDS)
            .writeTimeout(CONNECT_TIMEOUT_TIME, TimeUnit.SECONDS)
            .build();

    /**
     * gson 实例化了
     *
     * @return
     */
    public static OffLineApi getOffLineApi() {
        if (offLineApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(Url.URL + "/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            offLineApi = retrofit.create(OffLineApi.class);
        }
        return offLineApi;
    }


    /**
     * 还是json原型数据,没有实例化
     *
     * @return
     */
    public static OffLineApi getOffLineApiOfJson() {  // ----------> 泛型  ResponseBody
        if (offLineApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(Url.URL + "/")
                    .addConverterFactory(GsonConverterFactory.create()) // 加上这句话
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            offLineApi = retrofit.create(OffLineApi.class);
        }
        return offLineApi;
    }
}
